package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @program: gulimall
 * @description: TODO
 * @author: YuWenYi
 * @create: 2021-07-22 10:44
 **/
@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private  String app_id = "2021000117693327";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCGsoP2JRk/k1hcg89nHSSkRxkoqLbckjlEQ+BOWwRmBpdKkHvBhjBgpMVXHm+DziOTAK6HTCppWUJIanj81/6LzVoWYXKchhQiB4DIYRKcz7Kga3kc95XWNzDT7IrFZWpB6cil5acOKg0G+63eCSslLt8/n0/SL+5kBOSVBLzBUmdslqKfpWG7ZQqrfkcDKec5emN1LfJx65WYup6W252BhFen+P5nYPZNagUg1e0dA1K+6IK1aJ+McA/aK+Lc6OXeM86FQxmtBzUUe0Pe4mZdJCot5SpB2OfSXGA29wFhV3t5AKxHQFb3kqoRWtjYCQmpuwUZPeLvWWiBujM6oXQ9AgMBAAECggEAbXqc/SxsGBtXn2JW6dVrGcbb8QsoDaEfwJd2sdkSeyZyQ12p6MJ7gaGK7+NB+hxLF8lfEYLbmeK9mARKMCASuWiAB17O+9+Sb5Ivny4fJas+4VpZBy1I8OLM4gMJS6XBNq+gsQ42uLF/AyT+zVAgbElrlNeU1h61IUBE1cYS8QEm62nGOWcU6HYx0v54XrBvLDO+LsCgYec09I2OzHnzBm1qe5gtUwK5sn8wv8M8P+Raa8fbCkUqCBPz0d3cc89F8TY0Unzeh29D7kz+jb/pjkTtG77QqzvsDPwU5N0JuSmM04C1cxJO9ufEHUZWiNrW+lC2NF1ENd4tfVt0r4A/oQKBgQDnfHOOBMnVJLSoc/ppIuwKNau/tqOICh3g7tzO5nG951w4oirh5PirHPEzunyQQVguQBLtk3cgfKpczxM3IKJHsfH40BT0onXHyEQxmAQn2BjjGu13N2QfFIq8Jpz6zE5SoRzLCuchrQYFfA1QBQHR+tUnfq2sRN81vO5dIsvfpQKBgQCU9iLowlZV6ciquUZrqAR4lNBEWlZD7SDNP00RZq06EDLzETEuovtpL+t32uTjykNgDOv0ik3doySQqE5lNFrBj8/x6l35RFi1uJhXG/j5PZxJXQJCzAiHM+uc6usROfiGr0DHk+K9m19wEqokSr5IZcux0O5cILx05EBqKSGeuQKBgQDAJSZJySG7gWdPuE0v/ePNqbUxQinFnxgkAEFaoB/jkOqUu886iHP8nN59oJsKpqLKaTcM6u3kEnba6Ku7g6QQPN1sOVRbp4p6VXp9bABvDknCITThUyIRRlzORoLhDrsojUE4LImkA1nPLaRjOnTDViCeUuFX8CcTFQsKVfrCXQKBgF9op0Nd9d5hNt3ifXBG1VZGOn8NPWOUZgYQHVSf5yPhbECzKkUsrsrYnoDN+vzLeAv1OswOKYFkFO9yXYQE3HxNmGriKf6WABukeC6TBAzfoVJ7+A7AkFXIs4h2Dsmhz8S0S1GJLxkygxZNXhyQov/L84OYxpLLhOl5qKhWX+pBAoGAAzU7k72QkymGcX4TQt943hhwPPK+gla2PpdtKCwt7WuLetL63sPEgBNHBByOcucBG7LDO8dFBjur/iVr4zI4x+bc4WRxQYJ13Qz3BP2c+mUx6MwiL4Y+2wUib5fuk/jNFl4HEyAmpbQ8Pc8gm0/B9pV5DJPqb24kMOtaKJgKlpw=";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgM/riDEDEpZ/C2HM/vqwO+wFedl3E3Yzw1641ljGVfx3SH+7apLKytQqwss0AsYYp8T2DsCdQ+tUjPbkfZ0HEouoCgcpeSbNvBmJTLeNsbI7p29Nrv+zawwbfZLdhlzLBOo0Rcvj1SikoXv+25Y7055KCcEnA8br4OkoW0cdAy5MlI2A8Rg+LfYsHotdfoiKhCH1gn9n0f3P2LHK3ga4um51DK6No3YbP3tNIa4aR1/+N7E+srhxTht215GkI/Enza7IGxAqoQRWEjSoGDf1GQhUPNGzmg7CanCh3lQy/FSQ+DxWjesnT+XTszqxoF8uCQOjx6rO/79jDz3i6ZJ32wIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个Post请求，告诉我们支付成功的信息
    private  String notify_url = "http://o74uzysfik.52http.tech/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 自动关单时间
    private String timeout = "1m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        // 30分钟内不付款就会自动关单
        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"" + timeout + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        return result;
    }
}